package indi.youxiaozhong.http;

import android.content.Context;

import org.apache.commons.fileupload.FileItem;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import fi.iki.elonen.NanoHTTPD;

public class NanoHttpRequest {

    private final static String logTag = "NanoHttpServer";

    private final NanoHTTPD.IHTTPSession session;

    private final Context context;
    private final String rootPath;

    private final Map<String,List<FileItem>> files;


    private Map<String,Object> attribute;


    NanoHttpRequest(NanoHTTPD.IHTTPSession session,Context context,Map<String,List<FileItem>> files,String path){
        this.session = session;
        this.context = context;
        this.rootPath = path;
        this.files = files;

    }

    public List<FileItem> getFiles(String name){
        if(this.files == null) return null;
        return this.files.get(name);
    }

    public FileItem getFile(String name){
        List<FileItem> files =  this.getFiles(name);
        if(files!=null && files.size()>0){
            return files.get(0);
        }
        return null;
    }

    public Context getContext() {
        return context;
    }

    public NanoHTTPD.IHTTPSession getSession() {
        return session;
    }

    public String getUri(){
        return this.session.getUri();
    }

    public String getHeader(String name,String defaultValue){

        String value = this.session.getHeaders().get(name);
        if(value != null){
            return value;
        }
        return defaultValue;
    }

    public String getHeader(String name){
        return this.getHeader(name,null);
    }


    public Map<String,Object> getAttributes(){
        return this.attribute;
    }

    public void setAttribute(String name,Object value){
            if(attribute == null){
                attribute = new HashMap<String,Object>();
            }
            attribute.put(name,value);
    }

    public <T> T getAttribute(String name,T defaultValue){
        Object v = this.attribute.get(name);
        return (T)v;
    }

    public <T> T getAttribute(String name){
        return  this.getAttribute(name,null);
    }

    public List getParameters(String name){
        List<String> values = this.session.getParameters().get(name);
        if(values!=null && values.size()>0){
            return  values;
        }
        return null;
    }

    public Map<String,List<String>>  getParameterAll(){
        return  this.session.getParameters();
    }


    public String  getParameter(String name,String defaultValue){
            if(this.session.getParameters() == null) return defaultValue;
            List<String> values = this.session.getParameters().get(name);
            if(values!=null && values.size()>0){
                return  values.get(0);
            }
        return defaultValue;
    }

    public String getParameter(String name){
        return this.getParameter(name,null);
    }


    public RequestDispatcher getRequestDispatcher(String path){
        return new RequestDispatcher(path,this);
    }

    public class  RequestDispatcher{

        private NanoHttpRequest request;
        private String path;

        RequestDispatcher(String path,NanoHttpRequest request){
                this.path = path;
                this.request = request;
        }

        public String toHtml() throws IOException {
                return VelocityUtils.toHtml(NanoHttpRequest.this.rootPath+"/"+this.path, request.getAttributes());

        }

    }

}
